package com.atguigu.lease.admin.service.impl;

import com.atguigu.lease.admin.mapper.ApartmentFacilityMapper;
import com.atguigu.lease.admin.mapper.ApartmentFeeValueMapper;
import com.atguigu.lease.admin.mapper.ApartmentLabelMapper;
import com.atguigu.lease.admin.service.*;
import com.atguigu.lease.model.bean.*;
import com.atguigu.lease.model.enums.ItemTypeEnum;
import com.atguigu.lease.model.vo.*;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.lease.admin.mapper.ApartmentInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author lenovo
* @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
* @createDate 2025-06-20 19:11:57
*/
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
    implements ApartmentInfoService{
    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityMapper apartmentFacilityMapper;
    @Autowired
    private ApartmentLabelMapper apartmentLabelMapper;
    @Autowired
    private ApartmentFeeValueMapper apartmentFeeValueMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdateApartmentSubmitVo(ApartmentSubmitVo apartmentSubmitVo) {
        boolean isUpdate = apartmentSubmitVo.getId() != null;

        ProvinceInfo provinceInfo = provinceInfoService.getById(apartmentSubmitVo.getProvinceId());
        CityInfo cityInfo = cityInfoService.getById(apartmentSubmitVo.getCityId());
        DistrictInfo districtInfo = districtInfoService.getById(apartmentSubmitVo.getDistrictId());

        apartmentSubmitVo.setProvinceId(provinceInfo.getId());
        apartmentSubmitVo.setCityId(cityInfo.getId());
        apartmentSubmitVo.setDistrictId(districtInfo.getId());

        this.saveOrUpdate(apartmentSubmitVo);
        Long id = apartmentSubmitVo.getId();

        if(isUpdate){
            apartmentFacilityService.remove(Wrappers.lambdaQuery(ApartmentFacility.class)
                    .eq(ApartmentFacility::getApartmentId, id));
            apartmentLabelService.remove(Wrappers.lambdaQuery(ApartmentLabel.class)
                    .eq(ApartmentLabel::getApartmentId, id));
            apartmentFeeValueService.remove(Wrappers.lambdaQuery(ApartmentFeeValue.class)
                    .eq(ApartmentFeeValue::getApartmentId, id));
            graphInfoService.remove(Wrappers.lambdaQuery(GraphInfo.class)
                    .eq(GraphInfo::getId, id)
                    .eq(GraphInfo::getItemType, ItemTypeEnum.APARTMENT));
        }
        //配套
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        List<ApartmentFacility> apartmentFacilities = new ArrayList<>();
        facilityInfoIds.forEach(facilityInfoId -> {
            ApartmentFacility apartmentFacility = new ApartmentFacility();
            apartmentFacility.setApartmentId(id);
            apartmentFacility.setFacilityId(facilityInfoId);
            apartmentFacilities.add(apartmentFacility);
        });

        apartmentFacilityService.saveBatch(apartmentFacilities);

        //标签
        List<ApartmentLabel> apartmentLabels = apartmentSubmitVo.getLabelIds()
                .stream()
                .map(labelId -> {
                    ApartmentLabel apartmentLabel = new ApartmentLabel();
                    apartmentLabel.setApartmentId(id);
                    apartmentLabel.setLabelId(labelId);
                    return apartmentLabel;
                }).collect(Collectors.toList());

        apartmentLabelService.saveBatch(apartmentLabels);

        //杂费
        List<ApartmentFeeValue> apartmentFeeValues = apartmentSubmitVo.getFeeValueIds().stream()
                .map(feeValueId -> {
                    ApartmentFeeValue apartmentFeeValue = new ApartmentFeeValue();
                    apartmentFeeValue.setApartmentId(id);
                    apartmentFeeValue.setFeeValueId(feeValueId);
                    return apartmentFeeValue;
                }).collect(Collectors.toList());

        apartmentFeeValueService.saveBatch(apartmentFeeValues);

        //图片
        List<GraphInfo> graphInfos = apartmentSubmitVo.getGraphVoList().stream()
                .map(graphVo -> {
                    GraphInfo graphInfo = new GraphInfo();
                    graphInfo.setUrl(graphVo.getUrl());
                    graphInfo.setName(graphVo.getName());
                    graphInfo.setItemId(id);
                    graphInfo.setItemType(ItemTypeEnum.APARTMENT.getCode());
                    return graphInfo;
                }).collect(Collectors.toList());

        graphInfoService.saveBatch(graphInfos);
    }

    @Override
    public Page<ApartmentItemVo> getPageItem(Long current, Long size, ApartmentQueryVo queryVo) {

        return baseMapper.selectApartmentItemVoPage(new Page<>(current,size),queryVo);
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        ApartmentInfo apartmentInfo = this.getById(id);

        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();

        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);

        List<FacilityInfo> facilityInfos = apartmentFacilityMapper.selectFacilityInfosByApartmentId(id);
        apartmentDetailVo.setFacilityInfoList(facilityInfos);

        List<LabelInfo> labelInfos = apartmentLabelMapper.selectLabelInfsByApartmentId(id);
        apartmentDetailVo.setLabelInfoList(labelInfos);

        List<FeeValueVo> feeValueVos = apartmentFeeValueMapper.selectFeeValueVosByApartmentId(id);
        apartmentDetailVo.setFeeValueVoList(feeValueVos);

        List<GraphVo> graphVos = graphInfoService.list(Wrappers.lambdaQuery(GraphInfo.class)
                        .eq(GraphInfo::getId, id)
                        .eq(GraphInfo::getItemType, ItemTypeEnum.APARTMENT))
                .stream()
                .map(graphInfo -> {
                    GraphVo graphVo = new GraphVo();
                    /*graphVo.setUrl(graphInfo.getUrl());
                    graphVo.setName(graphInfo.getName());*/
                    BeanUtils.copyProperties(graphInfo, graphVo);
                    return graphVo;
                }).collect(Collectors.toList());
        apartmentDetailVo.setGraphVoList(graphVos);


        return apartmentDetailVo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteApartmentById(Long id) {
        this.removeById(id);

        apartmentFacilityService.remove(Wrappers.lambdaQuery(ApartmentFacility.class)
                .eq(ApartmentFacility::getApartmentId, id));

        apartmentLabelService.remove(Wrappers.lambdaQuery(ApartmentLabel.class)
                .eq(ApartmentLabel::getApartmentId, id));

        apartmentFeeValueService.remove(Wrappers.lambdaQuery(ApartmentFeeValue.class)
                .eq(ApartmentFeeValue::getApartmentId, id));

        graphInfoService.remove(Wrappers.lambdaQuery(GraphInfo.class)
                .eq(GraphInfo::getId, id)
                .eq(GraphInfo::getItemType, ItemTypeEnum.APARTMENT));
    }
}




